home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / markers.texi < prev    next >
Lisp/Scheme  |  1993-05-20  |  19KB  |  580 lines

  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. 
  4. @c See the file elisp.texi for copying conditions.
  5. @setfilename ../info/markers
  6. @node Markers, Text, Positions, Top
  7. @chapter Markers
  8. @cindex markers
  9.  
  10.   A @dfn{marker} is a Lisp object used to specify a position in a buffer
  11. relative to the surrounding text.  A marker changes its offset from the
  12. beginning of the buffer automatically whenever text is inserted or
  13. deleted, so that it stays with the two characters on either side of it.
  14.  
  15. @menu
  16. * Overview of Markers::      The components of a marker, and how it relocates.
  17. * Predicates on Markers::    Testing whether an object is a marker.
  18. * Creating Markers::         Making empty markers or markers at certain places.
  19. * Information from Markers:: Finding the marker's buffer or character position.
  20. * Changing Markers::         Moving the marker to a new buffer or position.
  21. * The Mark::                 How ``the mark'' is implemented with a marker.
  22. * The Region::               How to access ``the region''.
  23. @end menu
  24.  
  25. @node Overview of Markers, Predicates on Markers, Markers, Markers
  26. @section Overview of Markers
  27.  
  28.   A marker specifies a buffer and a position in that buffer.  The marker
  29. can be used to represent a position in the functions that require one,
  30. just as an integer could be used.  @xref{Positions}, for a complete
  31. description of positions.
  32.  
  33.   A marker has two attributes: the marker position, and the marker
  34. buffer.  The marker position is an integer which is equivalent (at the
  35. moment) to the marker as a position in that buffer; however, as text is
  36. inserted or deleted in the buffer, the marker is relocated, so that its
  37. integer equivalent changes.  The idea is that a marker positioned
  38. between two characters in a buffer will remain between those two
  39. characters despite any changes made to the contents of the buffer; thus,
  40. a marker's offset from the beginning of a buffer may change often during
  41. the life of the marker.
  42.  
  43. @cindex marker relocation
  44.   If the text around a marker is deleted, the marker is repositioned
  45. between the characters immediately before and after the deleted text.  If
  46. text is inserted at the position of a marker, the marker remains in front
  47. of the new text unless it is inserted with @code{insert-before-markers}
  48. (@pxref{Insertion}).  When text is inserted or deleted somewhere before the
  49. marker position (not next to the marker), the marker moves back and forth
  50. with the two neighboring characters.
  51.  
  52. @cindex marker garbage collection
  53.   When a buffer is modified, all of its markers must be checked so that
  54. they can be relocated if necessary.  This slows processing in a buffer
  55. with a large number of markers.  For this reason, it is a good idea to
  56. make a marker point nowhere if you are sure you don't need it any more.
  57. Unreferenced markers will eventually be garbage collected, but until
  58. then will continue to be updated if they do point somewhere.
  59.  
  60. @cindex markers as numbers
  61.   Because it is quite common to perform arithmetic operations on a marker
  62. position, most of the arithmetic operations (including @code{+} and
  63. @code{-}) accept markers as arguments.  In such cases, the current position
  64. of the marker is used.
  65.  
  66. Here are examples of creating markers, setting markers, and moving point
  67. to markers:
  68.  
  69. @example
  70. @group
  71. ;; @r{Make a new marker that initially does not point anywhere:}
  72. (setq m1 (make-marker))
  73.      @result{} #<marker in no buffer>
  74. @end group
  75.  
  76. @group
  77. ;; @r{Set @code{m1} to point between the 100th and 101st characters}
  78. ;;   @r{in the current buffer:}
  79. (set-marker m1 100)
  80.      @result{} #<marker at 100 in markers.texi>
  81. @end group
  82.  
  83. @group
  84. ;; @r{Now insert one character at the beginning of the buffer:}
  85. (goto-char (point-min))
  86.      @result{} 1
  87. (insert "Q")
  88.      @result{} nil
  89. @end group
  90.  
  91. @group
  92. ;; @r{@code{m1} is updated appropriately.}
  93. m1
  94.      @result{} #<marker at 101 in markers.texi>
  95. @end group
  96.  
  97. @group
  98. ;; @r{Two markers that point to the same position}
  99. ;;   @r{are not @code{eq}, but they are @code{equal}.}
  100. (setq m2 (copy-marker m1))
  101.      @result{} #<marker at 101 in markers.texi>
  102. (eq m1 m2)
  103.      @result{} nil
  104. (equal m1 m2)
  105.      @result{} t
  106. @end group
  107.  
  108. @group
  109. ;; @r{When you are finished using a marker, make it point nowhere.}
  110. (set-marker m1 nil)
  111.      @result{} #<marker in no buffer>
  112. @end group
  113. @end example
  114.  
  115. @node Predicates on Markers, Creating Markers, Overview of Markers, Markers
  116. @section Predicates on Markers
  117.  
  118.   You can test an object to see whether it is a marker, or whether it is
  119. either an integer or a marker.  The latter test is useful when you are
  120. using the arithmetic functions that work with both markers and integers.
  121.  
  122. @defun markerp object
  123.   This function returns @code{t} if @var{object} is a marker,
  124. @code{nil} otherwise.  In particular, integers are not markers,
  125. even though many functions will accept either a marker or an
  126. integer.
  127. @end defun
  128.  
  129. @defun integer-or-marker-p object
  130.   This function returns @code{t} if @var{object} is an integer or a marker,
  131. @code{nil} otherwise.
  132. @end defun
  133.  
  134. @defun number-or-marker-p object
  135.   This function returns @code{t} if @var{object} is a number (of any
  136. type) or a marker, @code{nil} otherwise.
  137. @end defun
  138.  
  139. @node Creating Markers, Information from Markers, Predicates on Markers, Markers
  140. @section Functions That Create Markers
  141.  
  142.   When you create a new marker, you can make it point nowhere, or point
  143. to the present position of point, or to the beginning or end of the
  144. accessible portion of the buffer, or to the same place as another given
  145. marker.
  146.  
  147. @defun make-marker
  148.   This functions returns a newly allocated marker that does not point
  149. anywhere.
  150.  
  151. @example
  152. @group
  153. (make-marker)
  154.      @result{} #<marker in no buffer>
  155. @end group
  156. @end example
  157. @end defun
  158.  
  159. @defun point-marker
  160.   This function returns a new marker that points to the present position
  161. of point in the current buffer.  @xref{Point}.  For an example, see
  162. @code{copy-marker}, below.
  163. @end defun
  164.  
  165. @defun point-min-marker
  166.   This function returns a new marker that points to the beginning of the
  167. accessible portion of the buffer.  This will be the beginning of the
  168. buffer unless narrowing is in effect.  @xref{Narrowing}.
  169. @end defun
  170.  
  171. @defun point-max-marker
  172. @cindex end of buffer marker
  173.   This function returns a new marker that points to the end of the
  174. accessible portion of the buffer.  This will be the end of the buffer
  175. unless narrowing is in effect.  @xref{Narrowing}.
  176.  
  177. Here are examples of this function and @code{point-min-marker}, shown in
  178. a buffer containing a version of the source file for the text of this
  179. chapter.
  180.  
  181. @example
  182. @group
  183. (point-min-marker)
  184.      @result{} #<marker at 1 in markers.texi>
  185. (point-max-marker)
  186.      @result{} #<marker at 15573 in markers.texi>
  187. @end group
  188.  
  189. @group
  190. (narrow-to-region 100 200)
  191.      @result{} nil
  192. @end group
  193. @group
  194. (point-min-marker)
  195.      @result{} #<marker at 100 in markers.texi>
  196. @end group
  197. @group
  198. (point-max-marker)
  199.      @result{} #<marker at 200 in markers.texi>
  200. @end group
  201. @end example
  202. @end defun
  203.  
  204. @defun copy-marker marker-or-integer
  205.   If passed a marker as its argument, @code{copy-marker} returns a
  206. new marker that points to the same place and the same buffer as does
  207. @var{marker-or-integer}.  If passed an integer as its argument,
  208. @code{copy-marker} returns a new marker that points to position
  209. @var{marker-or-integer} in the current buffer.
  210.  
  211.   If passed an argument that is an integer whose value is less than 1,
  212. @code{copy-marker} returns a new marker that points to the
  213. beginning of the current buffer.  If passed an argument that is an
  214. integer whose value is greater than the length of the buffer, then
  215. @code{copy-marker} returns a new marker that points to the end of the
  216. buffer.
  217.  
  218.   An error is signaled if @var{marker} is neither a marker nor an
  219. integer.
  220.  
  221. @example
  222. @group
  223. (setq p (point-marker))
  224.      @result{} #<marker at 2139 in markers.texi>
  225. @end group
  226.  
  227. @group
  228. (setq q (copy-marker p))
  229.      @result{} #<marker at 2139 in markers.texi>
  230. @end group
  231.  
  232. @group
  233. (eq p q)
  234.      @result{} nil
  235. @end group
  236.  
  237. @group
  238. (equal p q)
  239.      @result{} t
  240. @end group
  241.  
  242. @group
  243. (copy-marker 0)
  244.      @result{} #<marker at 1 in markers.texi>
  245. @end group
  246.  
  247. @group
  248. (copy-marker 20000)
  249.      @result{} #<marker at 7572 in markers.texi>
  250. @end group
  251. @end example
  252. @end defun
  253.  
  254. @node Information from Markers, Changing Markers, Creating Markers, Markers
  255. @section Information from Markers
  256.  
  257.   This section describes the functions for accessing the components of a
  258. marker object.
  259.  
  260. @defun marker-position marker
  261.   This function returns the position that @var{marker} points to, or
  262. @code{nil} if it points nowhere.
  263. @end defun
  264.  
  265. @defun marker-buffer marker
  266.   This function returns the buffer that @var{marker} points into, or
  267. @code{nil} if it points nowhere.
  268.  
  269. @example
  270. @group
  271. (setq m (make-marker))
  272.      @result{} #<marker in no buffer>
  273. @end group
  274. @group
  275. (marker-position m)
  276.      @result{} nil
  277. @end group
  278. @group
  279. (marker-buffer m)
  280.      @result{} nil
  281. @end group
  282.  
  283. @group
  284. (set-marker m 3770 (current-buffer))
  285.      @result{} #<marker at 3770 in markers.texi>
  286. @end group
  287. @group
  288. (marker-buffer m)
  289.      @result{} #<buffer markers.texi>
  290. @end group
  291. @group
  292. (marker-position m)
  293.      @result{} 3770
  294. @end group
  295. @end example
  296. @end defun
  297.  
  298.   Two distinct markers will be found @code{equal} (even though not
  299. @code{eq}) to each other if they have the same position and buffer, or
  300. if they both point nowhere.
  301.  
  302. @node Changing Markers, The Mark, Information from Markers, Markers
  303. @section Changing Markers
  304.  
  305.   This section describes how to change the position of an existing
  306. marker.  When you do this, be sure you know whether the marker is used
  307. outside of your program, and, if so, what effects will result from
  308. moving it---otherwise, confusing things may happen in other parts of
  309. Emacs.
  310.  
  311. @defun set-marker marker position &optional buffer
  312.   This function moves @var{marker} to @var{position}
  313. in @var{buffer}.  If @var{buffer} is not provided, it defaults to
  314. the current buffer.
  315.  
  316.   If @var{position} is less than 1, @code{set-marker} moves marker to
  317. the beginning of the buffer.  If the value of @var{position} is greater
  318. than the size of the buffer, @code{set-marker} moves marker to the end
  319. of the buffer.  If @var{position} is @code{nil} or a marker that points
  320. nowhere, then @var{marker} is set to point nowhere.
  321.  
  322.   The value returned is @var{marker}.
  323.  
  324. @example
  325. @group
  326. (setq m (point-marker))
  327.      @result{} #<marker at 4714 in markers.texi>
  328. @end group
  329. @group
  330. (set-marker m 55)
  331.      @result{} #<marker at 55 in markers.texi>
  332. @end group
  333. @group
  334. (setq b (get-buffer "foo"))
  335.      @result{} #<buffer foo>
  336. @end group
  337. @group
  338. (set-marker m 0 b)
  339.      @result{} #<marker at 1 in foo>
  340. @end group
  341. @end example
  342. @end defun
  343.  
  344. @defun move-marker marker position &optional buffer
  345.   This is another name for @code{set-marker}.
  346. @end defun
  347.  
  348. @node The Mark, The Region, Changing Markers, Markers
  349. @section The Mark
  350. @cindex mark, the
  351. @cindex mark ring
  352.  
  353.   A special marker in each buffer is designated @dfn{the mark}.  It
  354. records a position for the user for the sake of commands such as
  355. @kbd{C-w} and @kbd{C-x @key{TAB}}.  Lisp programs should set the mark
  356. only to values that have a potential use to the user, and never for
  357. their own internal purposes.  For example, the @code{replace-regexp}
  358. command sets the mark to the value of point before doing any
  359. replacements, because this enables the user to move back there
  360. conveniently after the replace is finished.
  361.  
  362.   Many commands are designed so that when called interactively they
  363. operate on the text between point and the mark.  If you are writing such
  364. a command, don't examine the mark directly; instead, use
  365. @code{interactive} with the @samp{r} specification.  This will provide
  366. the values of point and the mark as arguments to the command in an
  367. interactive call, but will permit other Lisp programs to specify
  368. arguments explicitly.  @xref{Interactive Codes}.
  369.  
  370.   Each buffer has its own value of the mark that is independent of the
  371. value of the mark in other buffers.  When a buffer is created, the mark
  372. exists but does not point anywhere.  We consider this state as ``the
  373. absence of a mark in that buffer''.
  374.  
  375.   Once the mark ``exists'' in a buffer, it normally never ceases to
  376. exist.  However, it may become @dfn{inactive}, if Transient Mark mode is
  377. enabled.  The variable @code{mark-active}, which is always local in all
  378. buffers, indicates whether the mark is active: non-@code{nil} means
  379. yes.  A command can request deactivation of the mark upon return to the
  380. editor command loop by setting @code{deactivate-mark} to a
  381. non-@code{nil} value (but this deactivation only follows if Transient
  382. Mark mode is enabled).
  383.  
  384.   The main motivation for using Transient Mark mode is that this mode
  385. also enables highlighting of the region when the mark is active.
  386. @xref{Emacs Display}.
  387.  
  388.   In addition to the mark, each buffer has a @dfn{mark ring} which is a
  389. list of markers that are the previous values of the mark.  When editing
  390. commands change the mark, they should normally save the old value of the
  391. mark on the mark ring.  The mark ring may contain no more than the
  392. maximum number of entries specified by the variable @code{mark-ring-max};
  393. excess entries are discarded on a first-in-first-out basis.
  394.  
  395. @defun mark &optional force
  396. @cindex current buffer mark
  397.   This function returns the position of the current buffer's mark as an
  398. integer.
  399.  
  400. Normally, if the mark is inactive @code{mark} signals an error.
  401. However, if @var{force} is non-@code{nil}, then it returns the mark
  402. position anyway---or @code{nil}, if the mark is not yet set for this
  403. buffer.
  404. @end defun
  405.  
  406. @defun mark-marker
  407.   This function returns the current buffer's mark.  This is the very marker
  408. which records the mark location inside Emacs, not a copy.  Therefore,
  409. changing this marker's position will directly affect the position of the mark.
  410. Don't do it unless that is the effect you want.
  411.  
  412. @example
  413. @group
  414. (setq m (mark-marker))
  415.      @result{} #<marker at 3420 in markers.texi>
  416. @end group
  417. @group
  418. (set-marker m 100)
  419.      @result{} #<marker at 100 in markers.texi>
  420. @end group
  421. @group
  422. (mark-marker)
  423.      @result{} #<marker at 100 in markers.texi>
  424. @end group
  425. @end example
  426.  
  427. Like any marker, this marker can be set to point at any buffer you like.
  428. We don't recommend that you make it point at any buffer other than the
  429. one of which it is the mark.  If you do, it will yield perfectly
  430. consistent, if rather odd, results.
  431. @end defun
  432.  
  433. @ignore
  434. @deffn Command set-mark-command jump
  435.   If @var{jump} is @code{nil}, this command sets the mark to the value
  436. of point and pushes the previous value of the mark on the mark ring.  The
  437. message @samp{Mark set} is also displayed in the echo area.
  438.  
  439.   If @var{jump} is not @code{nil}, this command sets point to the value
  440. of the mark, and sets the mark to the previous saved mark value, which
  441. is popped off the mark ring.
  442.  
  443.   This function is @emph{only} intended for interactive use.
  444. @end deffn
  445. @end ignore
  446.  
  447. @defun set-mark position
  448.   This function sets the mark to @var{position}, and activates the mark.
  449. The old value of the mark is @emph{not} pushed onto the mark ring.
  450.  
  451.   @strong{Please note:} use this function only if you want the user to
  452. see that the mark has moved, and you want the previous mark position to
  453. be lost.  Normally, when a new mark is set, the old one should go on the
  454. @code{mark-ring}.  For this reason, most applications should use
  455. @code{push-mark} and @code{pop-mark}, not @code{set-mark}.
  456.  
  457.   Novice Emacs Lisp programmers often try to use the mark for the wrong
  458. purposes.  The mark saves a location for the user's convenience.  An
  459. editing command should not alter the mark unless altering the mark is
  460. part of the user-level functionality of the command.  (And, in that
  461. case, this effect should be documented.)  To remember a location for
  462. internal use in the Lisp program, store it in a Lisp variable.  For
  463. example:
  464.  
  465. @example
  466. @group
  467. (let ((beg (point)))
  468.   (forward-line 1)
  469.   (delete-region beg (point))).
  470. @end group
  471. @end example
  472. @end defun
  473.  
  474. @c for interactive use only
  475. @ignore
  476. @deffn Command exchange-point-and-mark
  477.   This function exchanges the positions of point and the mark.
  478. It is intended for interactive use.
  479. @end deffn
  480. @end ignore
  481.  
  482. @defvar mark-ring
  483.   The value of this buffer-local variable is the list of saved former
  484. marks of the current buffer, most recent first.
  485.  
  486. @example
  487. @group
  488. mark-ring
  489. @result{} (#<marker at 11050 in markers.texi> 
  490.     #<marker at 10832 in markers.texi>
  491.     @dots{})
  492. @end group
  493. @end example
  494. @end defvar
  495.  
  496. @defopt mark-ring-max
  497. The value of this variable is the maximum size of @code{mark-ring}.
  498. If more marks than this are pushed onto the @code{mark-ring}, it
  499. discards marks on a first-in, first-out basis.
  500. @end defopt
  501.  
  502. @defun push-mark &optional position nomsg activate
  503. This function sets the current buffer's mark to @var{position}, and
  504. pushes a copy of the previous mark onto @code{mark-ring}.  If
  505. @var{position} is @code{nil}, then the value of point is used.
  506. @code{push-mark} returns @code{nil}.
  507.  
  508. The function @code{push-mark} normally @emph{does not} activate the
  509. mark.  To do that, specify @code{t} for the argument @var{activate}.
  510.  
  511. A @samp{Mark set} message is displayed unless @var{nomsg} is
  512. non-@code{nil}.
  513. @end defun
  514.  
  515. @defun pop-mark
  516. This function pops off the top element of @code{mark-ring} and makes
  517. that mark become the buffer's actual mark.  This does not change the
  518. buffer's point, and does nothing if @code{mark-ring} is empty.  It
  519. deactivates the mark.
  520.  
  521. The return value is not useful.
  522. @end defun
  523.  
  524. @defopt transient-mark-mode
  525. @cindex Transient Mark mode
  526. This variable enables Transient Mark mode, in which every
  527. buffer-modifying primitive sets @code{deactivate-mark}.  The consequence
  528. of this is that commands that modify the buffer normally cause the mark
  529. to become inactive.
  530. @end defopt
  531.  
  532. @defvar deactivate-mark
  533. If an editor command sets this variable non-@code{nil}, then the editor
  534. command loop deactivates the mark after the command returns.
  535. @end defvar
  536.  
  537. @defvar mark-active
  538. The mark is active when this variable is non-@code{nil}.  This variable
  539. is always local in each buffer.
  540. @end defvar
  541.  
  542. @defvar activate-mark-hook
  543. @defvarx deactivate-mark-hook
  544. These normal hooks are run, respectively, when the mark becomes active
  545. and when it becomes inactive.  The hook @code{activate-mark-hook} is also
  546. run at the end of a command if the mark is active and the region may
  547. have changed.
  548. @end defvar
  549.  
  550. @node The Region,  , The Mark, Markers
  551. @section The Region
  552. @cindex region, the
  553.  
  554.   The text between point and the mark is known as @dfn{the region}.
  555. Various functions operate on text delimited by point and the mark, but
  556. only those functions specifically related to the region itself are
  557. described here.
  558.  
  559. @defun region-beginning
  560.   This function returns the position of the beginning of the region (as
  561. an integer).  This is the position of either point or the mark,
  562. whichever is smaller.
  563.  
  564.   If the mark does not point anywhere, an error is signaled.
  565. @end defun
  566.  
  567. @defun region-end
  568.   This function returns the position of the end of the region (as an
  569. integer).  This is the position of either point or the mark, whichever is
  570. larger.
  571.  
  572.   If the mark does not point anywhere, an error is signaled.
  573. @end defun
  574.  
  575.   Few programs need to use the @code{region-beginning} and
  576. @code{region-end} functions.  A command designed to operate on a region
  577. should instead use @code{interactive} with the @samp{r} specification,
  578. so that the same function can be called with explicit bounds arguments
  579. from programs.  (@xref{Interactive Codes}.)
  580.